package com.gasolinator.a.app.activity.map;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.gasolinator.a.R;
import com.gasolinator.a.app.activity.GasolinatorPreferencesActivity;
import com.gasolinator.a.app.activity.base.IGasolinator;
import com.gasolinator.a.app.helper.GasolinatorPreferences;
import com.gasolinator.a.app.helper.GeoPointHelper;
import com.gasolinator.a.augmented_reality.data.ARData;
import com.gasolinator.a.augmented_reality.data.GasolinatorDataSource;
import com.gasolinator.a.augmented_reality.ui.Marker;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import java.util.List;

public class GasolinatorMapActivity extends MapActivity implements IGasolinator
{
    boolean detectLocation;
    GasolinatorOverlay gasolinatorOverLay;
    MapView mapView;
    FixedMyLocationOverlay myLocationOverlay;

    private List<Marker> gasolinatorMarkers;
    private String combustible;
    private GasolinatorDataSource gasolinatorDataSource;
    private GasolinatorPreferences gasolinatorPreferences;

    protected boolean isRouteDisplayed()
    {
        return false;
    }

    public void onCreate(Bundle param)
    {
        super.onCreate(param);
        requestWindowFeature(1);
        requestWindowFeature(2);
        int i = getWindowManager().getDefaultDisplay().getOrientation();
        if ((i == 1) || (i == 3)) {
            setContentView(R.layout.gas_station_map_horizontal);
        } else {
            setContentView(R.layout.gas_station_map);
        }
        this.mapView = (MapView)findViewById(R.id.mapview);
        this.mapView.setBuiltInZoomControls(true);
        this.mapView.setSatellite(false);
        this.mapView.getController().setZoom(13);
        List localList = this.mapView.getOverlays();

        this.myLocationOverlay = new FixedMyLocationOverlay(this, this.mapView);
        localList.add(this.myLocationOverlay);

        this.gasolinatorOverLay = new GasolinatorOverlay(this);

        localList.add(this.gasolinatorOverLay);
        gasolinatorDataSource = GasolinatorDataSource.getInstance(getApplicationContext());
        gasolinatorPreferences = new GasolinatorPreferences(this);
    }

    public boolean onCreateOptionsMenu(Menu paramMenu)
    {
        super.onCreateOptionsMenu(paramMenu);
        paramMenu.add(0, 2, 0, R.string.menu_set_location).setIcon(R.drawable.mylocation);
        paramMenu.add(0, 1, 0, R.string.menu_set_location).setIcon(R.drawable.mylocation);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem paramMenuItem)
    {
        GeoPoint localGeoPoint = null;
        boolean i;
        switch (paramMenuItem.getItemId())
        {
            default:
                i = false;
            case 1:
            case 2:
        }
        while (true)
        {
            if (this.detectLocation) {
                localGeoPoint = this.myLocationOverlay.getMyLocation();
            }
            if (localGeoPoint == null) {
                localGeoPoint = GeoPointHelper.location2GeoPoint(ARData.getCurrentLocation());
            }
            if (localGeoPoint != null) {
                this.mapView.getController().animateTo(localGeoPoint);
            }
            i = true;
            //continue;
            localGeoPoint = GeoPointHelper.getCheapest(gasolinatorDataSource.getGasolinatorMarkers(), combustible);
            if (localGeoPoint != null) {
                mapView.getController().animateTo(localGeoPoint);
            }
            i = true;
            return true;
        }
    }

    protected void onPause()
    {
        super.onPause();
        this.myLocationOverlay.disableMyLocation();
    }

    protected void onResume()
    {
        super.onResume();
        boolean satellite = gasolinatorPreferences.getSatellite();
        boolean fullcreen = gasolinatorPreferences.getFullscreen();
        mapView.setSatellite(satellite);
        if (fullcreen) {
            getWindow().setFlags(1024, 1024);
        }

        this.myLocationOverlay.enableMyLocation();

        final GeoPoint currentLocation = GeoPointHelper.location2GeoPoint(ARData.getCurrentLocation());
        if (currentLocation != null) {
            mapView.getController().setCenter(currentLocation);
            getWindow().setFlags(0, 1024);
        }
    }


    public void onBackAction(View view) {
        finish();
    }

    public void onPreferencesAction(View view) {
        Intent localIntent = new Intent(this, GasolinatorPreferencesActivity.class);
        startActivity(localIntent);
    }
}